package com.zzyl.controller.customer;


import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ElderBindDto;
import com.zzyl.exception.BusinessException;
import com.zzyl.service.CustomerMemberElderService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MemberElderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/customer/memberElder")
@Api(tags = "家属管理")
@Slf4j
public class CustomerMemberElderController {

    @Autowired
    private CustomerMemberElderService customerMemberElderService;

    @PostMapping("/add")
    @ApiOperation("添加/绑定家人")
    public ResponseResult<String> addFamily(@Valid @RequestBody ElderBindDto elderBindDto) {
        try {
            String result = customerMemberElderService.addOrBindFamily(elderBindDto);
            return ResponseResult.success(result);
        } catch (BusinessException e) {
            return ResponseResult.error(e.getMessage());
        } catch (Exception e) {
            log.error("添加家人时发生系统错误", e);
            return ResponseResult.error("系统错误，请稍后重试");
        }
    }

    @GetMapping("/my")
    @ApiOperation("获取我的家人列表")
    public ResponseResult<List<MemberElderVo>> getMyFamily() {
        try {
            List<MemberElderVo> memberElders = customerMemberElderService.getMyFamily();
            return ResponseResult.success(memberElders);
        } catch (Exception e) {
            log.error("获取家人列表失败", e);
            return ResponseResult.error("获取失败");
        }
    }

    @GetMapping("/list-by-page")
    @ApiOperation("分页查询家人列表")
    public ResponseResult<PageResponse<MemberElderVo>> getByPage(
            @ApiParam(value = "当前页码")
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示数量")
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Long memberId = UserThreadLocal.getUserId();
        PageResponse<MemberElderVo> memberElderVotPageInfo = customerMemberElderService.getMyFamilyByPage(memberId, pageNum, pageSize);
        return ResponseResult.success(memberElderVotPageInfo);
    }
}
